<?php
/**
 * @auther:周卫星
 * @className:GoodsController.php
 * @projectName:train
 * @classDesc:
 * @date:2019/11/1
 * @time:16:21
 */

namespace App\Http\Controllers\biz;


use App\Comment\Comment;
use App\Http\Controllers\Controller;
use App\Model\MallGoods;
use App\Model\MallGoodsType;
use App\Model\MallStoreGoodsClass;
use Illuminate\Http\Request;

class GoodsController extends Controller
{
    public $mall_goods;
    public $mall_store_goods_class;
    public $mall_goods_type;

    public function __construct()
    {
        $this->mall_goods = new MallGoods();
        $this -> mall_store_goods_class = new MallStoreGoodsClass();
        $this -> mall_goods_type = new MallGoodsType();
    }

    /**
     * 上架菜品展示
     * @param Request $request
     * @return GoodsController|\Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function index(Request $request)
    {
        if ($request->isMethod('get') && !$request->ajax()) {
            $mall_goods_class = $this -> mall_store_goods_class -> getGoodsClassList();
            $mall_goods_type = $this -> mall_goods_type -> getGoodsTypeList();
            return view('biz.goods.index', ['class_list' => $mall_goods_class, 'goods_type' => $mall_goods_type]);
        }
        if ($request->isMethod('get') && $request->ajax()) {
            $goods_name = $request->get('name', '');
            $sgc = $request->get('sgc_id', '');
            $gt = $request->get('gt_id', '');
            $state = $request->get('state', 1);
            $list = $this->mall_goods->getGoodsList($goods_name, $sgc, $gt, $state);
            $result = [
                'code' => 0,
                'message' => '获取成功',
                'count' => $list['total'],
                'data' => $list['data']
            ];
            return $this->returnJson($result);
        }
    }

    /**
     * 修改菜品
     * @param Request $request
     * @return GoodsController
     */
    public function updateGoods(Request $request)
    {
        if ($request -> isMethod('post')){
            // 获取菜品的主键ID
            $goods_id = $request -> post('id');
            // 或修改的数据
            $data = $request -> post('data');
            // 获取菜单分类
            if ($this -> mall_goods -> updateGoods($goods_id, $data)){
                $result = [
                    'code' => 0,
                    'message' => '修改成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'message' => '修改失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    public function getSgcList(Request $request)
    {
        if ($request->isMethod('get') && !$request->ajax()){
            return view('biz.goods.sgc');
        }
        if ($request->isMethod('get') && $request->ajax()) {
            $list = $this -> mall_store_goods_class -> getGoodsClassListPage();
            $result = [
                'code' => 0,
                'message' => '获取成功',
                'count' => $list['total'],
                'data' => $list['data']
            ];
            return $this->returnJson($result);
        }
    }

    /**
     * 添加菜品信息
     * @param Request $request
     * @return GoodsController
     */
    public function addGoodsInfo(Request $request)
    {
        if ($request -> isMethod('post')){
            $data = $request -> post('data');
            $data['gmt_create'] = date('Y-m-d H:i:s', time());
            $data['store_id'] = Comment::getBizId();
            if($this -> mall_goods -> addGoods($data)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 添加菜品分类
     * @param Request $request
     * @return GoodsController
     */
    public function addStoreGoodsClass(Request $request)
    {
        if ($request -> isMethod('post')){
            $data = $request -> post('data');
            $data['store_id'] = Comment::getBizId();
            if($this -> mall_store_goods_class -> addStoreGoodsClass($data)){
                $result = [
                    'code' => 0,
                    'msg' => '添加成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '添加失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 删除菜品分类信息
     * @param Request $request
     * @return GoodsController
     */
    public function delStoreGoodsClass(Request $request)
    {
        if ($request -> isMethod('post')){
            $data = $request -> post('data');
            $where = [
                'store_id' => Comment::getBizId(),
                'sgc_id' => $data['sgc_id']
            ];
            if($this -> mall_store_goods_class -> updateStoreGoodsClass($where, ['sgc_state' => 0])){
                $result = [
                    'code' => 0,
                    'msg' => '成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }

    /**
     * 修改菜品分类信息
     * @param Request $request
     * @return GoodsController
     */
    public function editStoreGoodsClass(Request $request)
    {
        if ($request -> isMethod('post')){
            $id = $request -> post('id');
            $data = $request -> post('data');
            $where = [
                'store_id' => Comment::getBizId(),
                'sgc_id' => $id
            ];
            if($this -> mall_store_goods_class -> updateStoreGoodsClass($where, $data)){
                $result = [
                    'code' => 0,
                    'msg' => '修改成功',
                    'data' => []
                ];
            }else{
                $result = [
                    'code' => 1,
                    'msg' => '修改失败',
                    'data' => []
                ];
            }
            return $this -> returnJson($result);
        }
    }
}
